package org.springsource.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesViewResolver;
import org.springsource.domain.AccountConverter;
import org.springsource.domain.Car;
import org.springsource.services.impl.AccountServiceImpl;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "org.springsource" })
@Import(PersistenceConfig.class)
public class WebConfig extends WebMvcConfigurerAdapter {

	@Bean
	public Car getFerrari() {
		Car fer = new Car();
		fer.setMake("ferrari");	
		return fer;
	}
	@Bean
	public TilesViewResolver configureTilesViewResolver() {
		return new TilesViewResolver();
	}

	@Bean
	public TilesConfigurer configureTilesConfigurer() {
		TilesConfigurer configurer = new TilesConfigurer();
		configurer.setDefinitions(new String[] {"/WEB-INF/tiles/tiles.xml", "/WEB-INF/views/**/views.xml"});
		return configurer;
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/").addResourceLocations("/recourses/**");
	}
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
	}
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	// Configuration for CRUD controller
		// See ~.crudcontroller

		@Override
		public void addFormatters(FormatterRegistry registry) {
			AccountConverter accountConverter = new AccountConverter(new AccountServiceImpl());
			registry.addConverter(accountConverter);
		}
}
